<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class Image extends Admin_Controller
{
		
	/**
	* called directly in browser
	*/
	function show_image()
	{
		global $CI;
		
		// model
		$CI->load->module_model('image.display_image_model');
		
		$id = $CI->uri->segment(3);
		$q = $CI->db->get_where(IMAGE, array( IMAGE_IMAGE_ID=>$id));
		$row = $q->row();
		
		// add to view
		$this->add('id', $id);
		$this->add('name', $row?$row->{IMAGE_NAME}:'');
		
		// view
		$this->use_global_template(FALSE);
		$this->view('show_image');
	}
	
	function lists()
	{
		global $CI;
		
		// model
		$CI->load->module_model('image.image_list_model');
		$CI->load->module_model('image.display_image_model');
		
		// filter	
	
		if (isset($_POST['filter']))
			$CI->image_list_model->cache->add($CI->image_list_model->filter_cache_prefix, $_POST['filter']);
		$ts = $CI->image_list_model->cache->load($CI->image_list_model->filter_cache_prefix);
		$CI->photoset_dropdown_model->value = $ts;

		// sql		
		
		$q = $CI->db->get(IMAGE);
		
		// pagenavigation
		$CI->load->library('pagination');
        $config['base_url'] = site_url($CI->image_list_model->url_prefix.'/%s');
        $config['total_rows'] = $q->num_rows();
        $config['per_page'] = $CI->config->item('rows_per_page');
		$config['cur_page'] = $CI->image_list_model->start;
		$config['num_links'] = $CI->config->item('num_links');		
        $CI->pagination->initialize($config);
        $this->add('paging', $CI->pagination->create_links());
		
		// sql
		$CI->db->from(IMAGE);
		$CI->db->select(IMAGE.'.'.IMAGE_IMAGE_ID.','.IMAGE.'.'.IMAGE_NAME);		
		foreach ($CI->image_list_model->columns as $k=>$v)
		{
			if ($CI->image_list_model->cache->load($k))
				$CI->db->order_by($v, $CI->image_list_model->cache->load($k));
		}
		$CI->db->limit($CI->config->item('rows_per_page'), $CI->image_list_model->start);
		$q = $CI->db->get();
		
		// gridview
		$this->add('items', $q->result());
		$this->add('start', $CI->image_list_model->start);
		foreach ($CI->image_list_model->columns as $k=>$v)
		{
			if ($CI->image_list_model->cache->load($k))
				$this->add('sort_'.$k,  'class="sort_column_'.$CI->image_list_model->cache->load($k).'"');
		}
		//Get image path
		$items = $q->result();
		$CI->load->library("fckeditor");	
		$path = $CI->fckeditor->UploadPath.'/images/';
		$this->add('image_franchise_path', $path);
						
		foreach($items as &$item){
			$item->image_path = $path.$item->{IMAGE_NAME};
		}
		
		// message
		$this->add('msg', $CI->image_list_model->cache->load($CI->image_list_model->msg_cache_prefix));
		$CI->image_list_model->cache->add($CI->image_list_model->msg_cache_prefix, '');
		
		// toolbar
		$this->module_toolbar('image.toolbar', array('parent_id'=>0, 'start'=>$CI->image_list_model->start));
		$this->module_navigation('Quản lý hình ');
		
		// view
		$this->view('image_list_view');
	}
	
	function detail()
	{
		global $CI;
		
		// model
		$CI->load->module_model('image.image_detail_model');
		
		// sql	
		$q = $CI->db->get_where(IMAGE, array(IMAGE_IMAGE_ID=>$CI->image_detail_model->item));
		$image = $q->row();
		
		// error
		$post = $CI->image_detail_model->cache->load('post');
		$CI->image_detail_model->cache->add('post', '');

		// add to view
		$this->add('name', $post?$post['name']:($image?$image->{IMAGE_NAME}:''));
		$this->add('caption', $post?$post['caption']:($image?$image->{IMAGE_CAPTION}:''));
		
		
		
		// add to view
		$this->add('start', $CI->image_detail_model->start);
		$this->add('item', $CI->image_detail_model->item);

		// message
		$this->add('msg', $CI->image_detail_model->cache->load($CI->image_detail_model->msg_cache_prefix));
		$CI->image_detail_model->cache->add($CI->image_detail_model->msg_cache_prefix, '');
		
		// toolbar
		$this->module_toolbar();
		if ($CI->image_detail_model->item)
			$this->module_navigation('Quản lý hình : Chỉnh sữa');
		else
			$this->module_navigation('Quản lý hình : Thêm mới');
			
		// view
		$this->view('image_detail_view');
	}
}

?>